<?php
namespace Seller\Controller;
use Think\Controller;
//备份用
defined('ROOT_PATH') or define('ROOT_PATH', dirname(realpath(APP_PATH)) . DS);
class CommonController extends Controller
{
    //如果未登录，就跳转到登录页面
    public function _initialize($params)
    {

        $login_shop_id = session('shop_id');
        if($login_shop_id <= 0 || !$login_shop_id){
            $this->redirect('Login/index');
            return false;
        }

        //判断是否过期
        $start_time =  M("shop")->where(array("id" => $login_shop_id))->getField("create_time");
        if(time() > $start_time + 31536000){
            // 清除缓存
            session('manager_id' ,null);
            session('role_id' ,null);
            session('avatar' ,null);
            session('name' ,null);
            session('mobile' ,null);
            session("role_name" ,null);
            session("shop_id",null);
            $this->error("您的店铺已到期，请及时续费",$_SERVER['HTTP_REFERER'],1);
        }
        $this->get_config();
        $this->get_nav();
        $c = strtolower(CONTROLLER_NAME);
        $a = strtolower(ACTION_NAME);
        $url = $c .'/'. $a;
        // 超级管理员
        $login_role = session("role_id");
        if($login_role == 1){
            return true;
        }
        // $params 参数请保证是全小写字母组成的以为数据
        // 例如 $params = array('admin/index' ,'system/index');
        if(in_array($a ,$params)){
            return true;
        }
        // 查询自己拥有的所有权限
        $my_permissions = M("permission")->where("role_id = ".session("role_id"))->select();
        // 未分配任何权限
        if($my_permissions){
            foreach($my_permissions as $val){
                $urls[] = $val['url'];
            }
            if(in_array($url ,$urls)){
                return true;
            }
        }
        // 错过了前面所有村
//        $this->error("您没有此权限！" , $_SERVER['HTTP_REFERER'],1);

        $this->redirect(U('Index/no_auth' ));
//        exit('您没有此权限！');
        return false;
    }

    // 加载默认配置
    public function get_config()
    {
        $configs = M("config")->where("shop_id = %d" ,array(session("shop_id")))->select();
        foreach($configs as $key => $config){
            $newConfig[$config['conf_key']] = $config['conf_value'];
        }
        $this->assign("config" ,$newConfig);
        $login_msg = M("manager")->where("id = %d",array(session("manager_id")))->field("last_login,last_ip")->find();
        $this->assign("login_msg" ,$login_msg);
    }

    // 读取导航
    public function get_nav()
    {
        $first_navs = M("navigation")->where("parent_id = 0 AND nav_status = 1 AND state = 1")
            ->order("nav_sort asc")->select();
        $son_navs = array();
        foreach($first_navs as $nk => $nv){
            $s_navs = M("navigation")
                ->where("parent_id = %d AND nav_status = 1 AND state = 1" ,array($nv['nav_id']))
                ->order("nav_sort asc")->field("nav_name,nav_c,nav_a")->select();

            $son_navs[$nk]["parent_id"] = $nv['nav_id'];
            $son_navs[$nk]["p_nav_name"] = $nv['nav_name'];
            $son_navs[$nk]["son_navs"] = $s_navs;
        }
        $this->assign("first_navs" ,$first_navs);
        $this->assign("son_navs" ,$son_navs);
//        echo "<pre>";print_r($son_navs);exit;
    }
}